using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Net;
using TNCGameEngine.Content;

namespace TNCGameEngine
{
    public abstract class Game : Microsoft.Xna.Framework.Game
    {

        #region "Properties"

        private NetworkSession network = null;
        public NetworkSession NetworkSession
        {
            get { return this.network; }
            protected set { this.network = value; }
        }

        private GraphicsDeviceManager graphics;
        public GraphicsDeviceManager Graphics
        {
            get { return this.graphics; }
        }

        private SharedContentManager content = null;
        public new SharedContentManager Content
        {
            get { return this.content; }
        }

        private ContentLoader contentLoader = null;
        public ContentLoader ContentLoader
        {
            get { return this.contentLoader; }
        }

        public LoadingScreen ActiveLoadingScreen
        {
            get { return this.contentLoader.LoadingScreen; }
            protected set { this.contentLoader.LoadingScreen = value; }
        }

        private int gameState = GameState.Initializing;
        public int CurrentGameState
        {
            get { return this.gameState; }
            set { this.gameState = value; }
        }              

        private float aspectRatio = 4.0f / 3.0f;
        public float AspectRatio
        {
            get { return this.aspectRatio; }
            set { this.aspectRatio = value; }
        }

        public int ScreenWidth
        {
            get { return this.GraphicsDevice.PresentationParameters.BackBufferWidth; }
        }

        public int ScreenHieght
        {
            get { return this.GraphicsDevice.PresentationParameters.BackBufferHeight; }
        }

        public bool IsNetworkReady
        {
            get { return (this.network != null); }
        }

        #endregion

        #region "Variables"

        
        #endregion

        #region "Methods"

        protected void PreInitialize(int nextGameState)
        {

            this.gameState = nextGameState;

            this.graphics = new GraphicsDeviceManager(this);
            
        }

        protected void InitializeNetwork(NetworkSessionType type, int maxLocalPlayers, int maxGamers, int privateGamerSlots, NetworkSessionProperties sessionProperties)
        {
            this.network = NetworkSession.Create(type, maxLocalPlayers, maxGamers, privateGamerSlots, sessionProperties);
        }

        protected override void Initialize()
        {

            base.Initialize();

            this.content = new SharedContentManager(base.Content.ServiceProvider, base.Content.RootDirectory);
            
            this.contentLoader = new ContentLoader(this);
            
            this.aspectRatio = (float)this.graphics.GraphicsDevice.Viewport.Width / this.graphics.GraphicsDevice.Viewport.Height;

        }
        
        protected override void LoadContent()
        {
            base.LoadContent();
            this.ActiveLoadingScreen.LoadContent();
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
            this.ActiveLoadingScreen.UnloadContent();
        }

        protected bool DrawLoadingScreen(GameTime gameTime)
        {

            if (this.gameState != GameState.Loading) { return false; }
            
            this.ActiveLoadingScreen.Draw(this.GraphicsDevice);
            return true;
            
        }

        #endregion

    }

    /// <summary>
    /// Defines a class that holds the game state information of which state to fall back to after the loading is complete.
    /// </summary>
    /// <remarks>New const values can be added to the derived class.</remarks>
    public class GameState
    {

        internal const int Initializing = 99990;
        public const int Loading = 99991;
        public const int Game = 99992;

    }

}
